package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;

import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {


    @Autowired
    private SupplierMapper supplierMapper;

    /**
     * 查询所有物资来源
     * @param pageNum 起始页
     * @param pageSize 每页显示条数
     * @param name  查询内容
     * @return
     */
    @Override
    public PageVO<SupplierVO> findSupplierList(int pageNum, int pageSize, String name) {
        List<Supplier> suppliers = new ArrayList<>();
        if ( ! StringUtils.isEmpty(name)){
            //如果条件不为空，就加上条件查询
            Example example = new Example(Supplier.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andLike("name","%"+name+"%");
            supplierMapper.selectByExample(example);
        }else {
            //如果为空就获取所有
            suppliers = supplierMapper.selectAll();
        }

        //判断

        ArrayList<SupplierVO> supplierVOList = new ArrayList<>();
        //判断
        if ( ! CollectionUtils.isEmpty(suppliers)){
            //循环判断
            for (Supplier supplier : suppliers) {
                SupplierVO supplierVO = new SupplierVO();
                BeanUtils.copyProperties(supplier,supplierVO);
                supplierVOList.add(supplierVO);
            }
            //分页条件
            PageHelper.startPage(pageNum,pageSize);
            PageInfo<SupplierVO> pageInfo = new PageInfo<>(supplierVOList);
            //返回page对象
            return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
        }
        return null;
    }


    /**
     * 添加物资来源
     * @param supplier
     * @return
     */
    @Override
    public void add(Supplier supplier) {
        //添加
        if (supplier != null){
            supplier.setCreateTime(new Date());
            supplier.setModifiedTime(new Date());
            supplierMapper.insert(supplier);
        }
    }


    /**
     * 数据回显
     * @param id
     * @return
     */
    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        if (supplier != null){
            SupplierVO supplierVO = new SupplierVO();
             BeanUtils.copyProperties(supplier,supplierVO);
            return supplierVO;
        }
        return null;
    }

    /**
     * 提交编辑信息
     * @param
     * @return
     */
    @Override
    public void update(Supplier supplier) {
        if (supplier != null){
            supplier.setModifiedTime(new Date());
            supplierMapper.updateByPrimaryKey(supplier);
        }
    }

    /**
     * 删除物资来源信息
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
       if (id != null && id != 0){
           supplierMapper.deleteByPrimaryKey(id);
       }
    }


    /**
     * 查询所有已知来源
     * @return
     */
    @Override
    public List<SupplierVO> findAll() {
        List<Supplier> suppliers = supplierMapper.selectAll();
        //判断
        if (suppliers != null){
            //封装
            List<SupplierVO> supplierVOList = new ArrayList<>();
            for (Supplier supplier : suppliers) {
                SupplierVO supplierVO = new SupplierVO();
                BeanUtils.copyProperties(supplier,supplierVO);
                supplierVOList.add(supplierVO);
            }
            return supplierVOList;
        }
        return null;
    }
}

